added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBEFEntityDataModel / One2Many / One2ManyClass.vb
blobd324e544eef6924e1c42f5ddb7e5c7519824db98
1 '****************************** Module Header ******************************'
2 ' Module Name: One2ManyClass.vb
3 ' Project: VBEFEntityDataModel
4 ' Copyright (c) Microsoft Corporation.
6 ' This example illustrates how to dinsert, update and query the two entities
7 ' with one to many association.
8 '
9 ' This source is subject to the Microsoft Public License.
10 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 ' All other rights reserved.
13 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 '***************************************************************************'
18 #Region "Imports directives"
19 Imports System
20 Imports System.Collections.Generic
21 Imports System.Linq
22 Imports System.Text
23 #End Region
26 Namespace VBEFEntityDataModel.One2Many
27 Friend Class One2ManyClass
29 ' Test all the methods in One2ManyClass
30 Public Shared Sub One2ManyTest()
31 InsertDepartmentWithCourse()
33 InsertCourse()
35 UpdateCourse()
37 UpdateDepartment()
39 End Sub
41 ' Insert new department with new course
42 Public Shared Sub InsertDepartmentWithCourse()
44 Using context As New EFO2MEntities()
46 Dim department As New Department() With _
47 { _
48 .DepartmentID = 4, _
49 .Name = "Software Engineering" _
52 Dim course As New Course() With _
53 { _
54 .CourseID = 2202, _
55 .Title = "ADO.NET" _
58 department.Course.Add(course)
60 context.AddToDepartment(department)
62 Try
64 Console.WriteLine("Inserting department {0} with course " _
65 + "{1}", department.Name, course.Title)
67 context.SaveChanges()
69 Query()
71 Catch ex As Exception
72 Console.WriteLine(ex.Message)
73 End Try
74 End Using
75 End Sub
78 ' Insert new course to existing department
79 Public Shared Sub InsertCourse()
81 Using context As New EFO2MEntities()
82 Dim course As New Course() With _
83 { _
84 .CourseID = 2203, _
85 .Title = "Object Oriented Programming" _
88 course.Department = (From p In context.Department _
89 Where p.DepartmentID = 7 _
90 Select p).First()
92 context.AddToCourse(course)
94 Try
96 Console.WriteLine("Inserting course {0} to department " _
97 + "{1}", course.Title, course.Department.Name)
99 context.SaveChanges()
101 Query()
103 Catch ex As Exception
104 Console.WriteLine(ex.Message)
105 End Try
106 End Using
107 End Sub
110 ' Get all the departments with courses
111 Public Shared Sub Query()
113 Using context As New EFO2MEntities()
115 Dim query = From p In context.Department.Include("Course") _
116 Select p
118 Console.WriteLine("Deparments with their courses:")
120 For Each d As Department In query
122 Console.WriteLine("{0} {1}", d.DepartmentID, d.Name)
124 For Each c As Course In d.Course
126 Console.WriteLine(" {0}", c.Title)
128 Next
129 Next
131 Console.WriteLine()
133 End Using
134 End Sub
137 ' Update one existing course
138 Public Shared Sub UpdateCourse()
140 Using context As New EFO2MEntities()
142 Dim course As New Course()
144 course.CourseID = 2203
146 context.AttachTo("Course", course)
148 course.Title = "OOP"
152 Console.WriteLine("Modifying Course 2203's Title to {0}", _
153 course.Title)
155 context.SaveChanges()
157 Query()
159 Catch ex As Exception
160 Console.WriteLine(ex.Message)
161 End Try
162 End Using
163 End Sub
166 ' Update one existing department
167 Public Shared Sub UpdateDepartment()
169 Using context As New EFO2MEntities()
171 Dim department As New Department()
173 department.DepartmentID = 1
175 context.AttachTo("Department", department)
177 department.Name = "Computer Engineering"
179 department.Course.Add(New Course() With _
181 .CourseID = 2204, _
182 .Title = "Arithmetic" _
187 Console.WriteLine("Modifying Department 1's Title to {0}" _
188 + ", and insert a new Course 2204 into the " + _
189 "Department 1", department.Name)
191 context.SaveChanges()
193 Query()
195 Catch ex As Exception
196 Console.WriteLine(ex.Message)
197 End Try
198 End Using
199 End Sub
201 End Class
203 End Namespace